package com.haredot.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.haredot.dto.AuditeDTO;
import com.haredot.dto.ResourceDTO;
import com.haredot.service.ResourceService;
import com.haredot.vo.R;
import com.haredot.vo.ResourceVo;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;

@RestController
@RequestMapping("/resource")
public class ResourceController {


    @Resource
    private ResourceService resourceService ;
    /**
     * 查询所有的资源列表信息
     * @param page
     * @param rows
     * @param resourceDTO
     */
    @GetMapping("/list")
    @PreAuthorize("hasAnyAuthority('resource:verify', 'resource:audited', 'resource:notApproved', 'resource:all')")
    public R<Page<ResourceVo>> list(@RequestParam(value = "page", defaultValue = "1" , required = false) Long page,
                     @RequestParam(value = "rows", defaultValue = "10", required = false) Long rows,
                     ResourceDTO resourceDTO) {

        // 创建一个 Page 分页对象
        Page<ResourceVo>  pageVo = new Page<>(page, rows) ;
        // 根据条件查询所有资源信息
        resourceService.queryResourcesByCondition(pageVo, resourceDTO) ;

        // 最后放到 R 中即可
        return R.<Page<ResourceVo>>ok().data(pageVo);
    }

    /**
     * 审核资源
     * @param auditeDTO
     * @return
     */
    @PutMapping("/audite")
    public R<?> auditeResource(@RequestBody @Valid AuditeDTO auditeDTO) {
        return resourceService.auditeResource(auditeDTO) ;
    }

    @DeleteMapping("/{resId}")
    public R<?> removeResource(@PathVariable("resId") Long resId) {
        return resourceService.removeResource(resId) ;
    }

    @PutMapping("/core/{resId}")
    public R<?> configCoreResource(@PathVariable("resId") Long resId) {
        return resourceService.configCoreResource(resId) ;
    }
}
